package edu.ccut.saturn.basecomponent.database.resultset.impl;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.ccut.saturn.basecomponent.database.resultset.IResultSetSet;
import edu.ccut.saturn.component.SaturnData;

public class ResultSetGetBinary implements IResultSetSet {

	public void setSaturnData(SaturnData saturnData, String key,
			ResultSet resultSet) throws SQLException {

		InputStream is = null;

		if ((is = resultSet.getBinaryStream(key)) != null) {

			BufferedInputStream bis = new BufferedInputStream(is);
			
			int length = bis.toString().length();

			byte[] value = new byte[length];

			try {

				bis.read(value);

			} catch (IOException e) {
				e.printStackTrace();
			} finally {

				// if (bufferedInputStream != null){
				// try {
				// bufferedInputStream.close();
				// } catch (IOException e) {
				// e.printStackTrace();
				// }
				// }

				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			saturnData.set(key, value);
		} else {
			saturnData.set(key, null);
		}
	}
}
